package com.itheima.d6_tcp3;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;

public class TheardServe implements Runnable {
    private Socket accept;

    public TheardServe(Socket accept) {
        this.accept = accept;
    }

    @Override
    public void run() {
        BufferedReader br = null;
        try {
            InputStream is = accept.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);

            // 4、把原始的字节输入流包装成高效的输入流
            br = new BufferedReader(isr);
            // 5、使用数据输入流读取客户端发送过来的消息
            while (true) {
                String s = br.readLine();
                if (s == null) {
                    break;
                }
                System.out.println(s);

            }
            // 6、释放资源

        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (br != null) {

                try {
                    br.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            try {
                accept.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }
    }
}
